/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-12-17     周童欢       the first version
 */
#include "drv_spi.h"
#include "spi_flash_sfud.h"
#include "dfs_fs.h"

static int rt_hw_spi_flash_init(void)
{
    __HAL_RCC_GPIOA_CLK_ENABLE();
    rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);

    if (RT_NULL == rt_sfud_flash_probe("W25Q128BV", "spi10"))
    {
        rt_kprintf("Failed to probe SPI Flash.\n");
        return -RT_ERROR;
    }

    rt_kprintf("SPI Flash initialized successfully.\n");
    return RT_EOK;
}
/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);

int dfs_mount_init(void)
{
    if (dfs_mount("W25Q128BV", "/", "elm", 0, 0) != 0)
    {
        rt_kprintf("No filesystem detected. Formatting...\n");
        dfs_mkfs("elm", "W25Q128BV");

        if (dfs_mount("W25Q128BV", "/", "elm", 0, 0) != 0)
        {
            rt_kprintf("Failed to mount the filesystem.\n");
            return -RT_ERROR;
        }
    }

    rt_kprintf("Filesystem mounted successfully.\n");
    return RT_EOK;
}
/* 导出到自动初始化 */
//INIT_COMPONENT_EXPORT(dfs_mount_init);
